﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Hotel.WPF.HotelWCFSurIIS;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;

namespace Hotel.WPF.ui.Chambre
{
    /// <summary>
    /// Interaction logic for P_Chambre.xaml
    /// </summary>
    public partial class P_Chambre : Page
    {
        HotelClient srvService;
        Chambre_Hotels _chambre;
        public P_Chambre(Chambre_Hotels chambre)
            : this()
        {
            
            try
            {
                this._chambre = chambre;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public P_Chambre()
        {
            InitializeComponent();
        }

        private void bt_ajoutChambre_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Chambre_Hotels chambre = new Chambre_Hotels();
                chambre.Hotel_ID = Convert.ToInt32(cb_hotelChamb.SelectedValue);
                chambre.Code_type_Chambre = Convert.ToInt32(cb_typeChamb.SelectedValue);
                chambre.Numero_Chambre = tb_numChambre.Text.Trim();
                chambre.Nombre_Etage_Chambre = Convert.ToInt32(tb_numEtageChamb.Text.Trim());
                chambre.Statut_ID = Convert.ToInt32(cb_statutChamb.SelectedValue);
                chambre.description_chambre = tb_descChamb.Text.Trim();
                chambre.Notes_Additionnelles = tb_noteAddiChamb.Text.Trim();

                //srvService = new HotelClient();
                if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                {
                    //writeToBusyTool("Connection au serveur...");
                    srvService.Open();
                }
                int resultatAjout = srvService.AddChambreHotels(chambre);

                if (resultatAjout > 0)
                {
                    MessageBox.Show("Enregistrement réussi",
                                    "Ajout Chambre",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                    loadListeChambre();
                }
                else
                {
                    MessageBox.Show("Erreur d'enresgistrement",
                                    "Ajout Chambre",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
                //srvService.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ajout Chambre", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        //chargement de la liste des chambres
        private void loadListeChambre()
        {
            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    writeToBusyTool("Chargement des chambre en cours...");
                    if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                    {
                        writeToBusyTool("Connection au serveur...");
                        srvService.Open();
                    }
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        var listeChambre = srvService.getListChambreHotels();
                                        dg_chambre.DataContext = listeChambre;
                                        //cb_hotelChamb.DataContext = listeHotel;
                                        //cb_hotelChamb.SelectedValuePath = "ID";
                                        //cb_hotelChamb.DisplayMemberPath = "Nom_Hotel";
                                    },
                                    null
                                    );


                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                        //srvService.Close();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Chambre", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        //initialisation des infos d'une chambre
        private void initInfoChambre()
        {
            try
            {
                cb_hotelChamb.SelectedValue = _chambre.Hotel_ID;
                cb_typeChamb.SelectedValue = _chambre.Code_type_Chambre;
                cb_statutChamb.SelectedValue = _chambre.Statut_ID;
                tb_numChambre.Text = _chambre.Numero_Chambre;
                tb_numEtageChamb.Text = _chambre.Nombre_Etage_Chambre.ToString();
                tb_descChamb.Text = _chambre.description_chambre;
                tb_noteAddiChamb.Text = _chambre.Notes_Additionnelles;
                bt_ajoutChambre.Visibility = System.Windows.Visibility.Hidden;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"initInfoChambre");
            }
        }

        private void writeToBusyTool(string message)
        {
            Dispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                (SendOrPostCallback)delegate
                {
                    busyTool.BusyContent = message;
                },
                null
                );
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            busyTool.Width = SystemParameters.WorkArea.Width;
            busyTool.Height = SystemParameters.WorkArea.Height;

            //srvService = new HotelClient();
            #region etablissement connexion avec serveur

            try
            {

                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    srvService = new HotelClient();

                    writeToBusyTool("Chargement des hotels en cours...");
                    //if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                    //{
                    //    writeToBusyTool("Connection au serveur...");
                    //    srvService.Open();
                    //}
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        var listeHotel = srvService.getListHotel();
                                        cb_hotelChamb.DataContext = listeHotel;
                                        cb_hotelChamb.SelectedValuePath = "ID";
                                        cb_hotelChamb.DisplayMemberPath = "Nom_Hotel";
                                    },
                                    null
                                    );

                    writeToBusyTool("Classement des types de chambre...");
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        cb_typeChamb.DataContext = srvService.getListRefTypeChambres();
                                        cb_typeChamb.SelectedValuePath = "ID";
                                        cb_typeChamb.DisplayMemberPath = "Nom_Type_Chambre";
                                    },
                                    null
                                    );

                    writeToBusyTool("Classement des statuts des chambres...");
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        cb_statutChamb.DataContext = srvService.getListRefStatutChambres();
                                        cb_statutChamb.SelectedValuePath = "ID";
                                        cb_statutChamb.DisplayMemberPath = "Nom_Statut_Chambre";
                                    },
                                    null
                                    );
                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        if (_chambre != null)
                                                                                        {
                                                                                            initInfoChambre();
                                                                                        }
                                                                                        loadListeChambre();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();
                //}

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            #endregion
        }

        private void gererListeChambre(object sender, RoutedEventArgs e)
        {

        }
    }
}
